#include <os.h>
#include <nCOMMON.h>
#include <nGUI.h>
#include "calc.h"
#define GUI_SDL
#include "gui_sdl_emu.h"

typedef unsigned char Uint8;
ScreenBuffer *buffer = NULL;

int main(int argc, char** argv) {
	buffer = GetNewScreenBuffer();
	clearScreen(BLACK, buffer);
	drawStr(0, 0 * CHAR_HEIGHT, "nRGBWabbitemu v0.00001 ALPHA", 1, 1, WHITE, buffer);
	drawStr(0, 1 * CHAR_HEIGHT, "=============================", 1, 1, WHITE, buffer);
	drawStr(0, 2 * CHAR_HEIGHT, "Ugly PoC Port by Albert H.", 1, 1, WHITE, buffer);
	drawStr(0, 4 * CHAR_HEIGHT, "Loading...", 1, 1, WHITE, buffer);
	display(buffer);
	sleep(2000);
	
	// Let's get rolling!
	printf("TPF: %d\n", TPF);
	
	drawStr(0, 5 * CHAR_HEIGHT, "Creating calculator slot...", 1, 1, WHITE, buffer);
	display(buffer);
	sleep(2000);
	
	//memset(frames, 0, sizeof(frames));
	lpCalc = calc_slot_new();
	
	drawStr(0, 6 * CHAR_HEIGHT, "Creating calculator...", 1, 1, WHITE, buffer);
	display(buffer);
	sleep(2000);
	
	lpCalc->SkinEnabled = false;
	//MyFrame *frame;
	int result = rom_load(lpCalc, lpCalc->rom_path);
	if (result == TRUE) {
		//frame = gui_frame(lpCalc);
	} else {
		drawStr(0, 7 * CHAR_HEIGHT, "Loading ROM...", 1, 1, WHITE, buffer);
		display(buffer);
		sleep(2000);
		calc_slot_free(lpCalc);
		//char *filePath = LoadRomIntialDialog();
		//char *filePath = "ti83p_112.rom";
		char *filePath = argv[1];
		//wxMessageBox(string, wxT("OnInit"), wxOK, NULL);
		//printf_d("hello", string);
		if (filePath != NULL) {
			lpCalc = calc_slot_new();
			result = rom_load(lpCalc, filePath);
			//delete filePath;
			if (result == TRUE) {
				//frame = gui_frame(lpCalc);
				initLCD();
				lpCalc->scale = 2;
				//lpCalc->active = TRUE;
				lpCalc->running = TRUE;
			} else {
				printf("[SDLWabbitemu] [start_main] Failed to load ROM!\n");
				return FALSE;
			}
		} else {
			return FALSE;
		}
	}
	
	drawStr(0, 7 * CHAR_HEIGHT, "Starting calculator...", 1, 1, WHITE, buffer);
	display(buffer);
	sleep(2000);
	
	bool emuRunning = true;
	
	// Get emulator core running
	printf("Initializing emulator core...\n");
	if (!(start_main())) {
		printf("[nRGBWabbitemu] Emulator core initilization failed, so exiting.\n");
		exit(1);
	}
	
	while (emuRunning) {
		OnTimer(NULL);
		display(buffer);
	}
	return 0;
}
